package com.gdk.subject.infra.basic.utils;

import com.alibaba.druid.filter.config.ConfigTools;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * 数据库加密Util
 * 
 * @author Gudong~
 * @create 2024-10-21 11:57
 */
public class DruidEncryptUtil {
    
    private static String publicKey;
    
    private static String privateKey;
    
    static {
        try {
            String[] keyPair = ConfigTools.genKeyPair(512);
            privateKey = keyPair[0];
            System.out.println("privateKey = " + privateKey);
            publicKey = keyPair[1];
            System.out.println("publicKey = " + publicKey);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchProviderException e) {
            throw new RuntimeException(e);
        }
    }

    public static String encrypt(String plainText) throws Exception {
        String encrypt = ConfigTools.encrypt(privateKey, plainText);
        //System.out.println("encrypt = " + encrypt);
        return encrypt;
    }

    public static String decrypt(String encryptText) throws Exception {
        String decrypt = ConfigTools.decrypt(publicKey, encryptText);
        //System.out.println("decrypt = " + decrypt);
        return decrypt;
    }

    public static void main(String[] args) throws Exception {
        String encrypt = encrypt("root");
        String encrypt2 = encrypt("123456");
        System.out.println("encrypt1 = " + encrypt);
        System.out.println("encrypt2 = " + encrypt2);
        String decrypt = decrypt(encrypt);
        String decrypt2 = decrypt(encrypt2);
        System.out.println("decrypt1 = " + decrypt);
        System.out.println("decrypt2 = " + decrypt2);
    }

}
